﻿// Copyright (c) 2013 Bryan C. O'Connell
// http://wsubi.codeplex.com/license
// Purpose: Defines a command that can be processed by wsubi.

using System;

namespace Wsubi
{
    class Command
    {
        /// <summary>Initialize a new Command object based in input.</summary>
        /// <param name="UserCommand">string to be converted into a Command.</param>
        public Command(string UserCommand) 
        {
            ParseCommand(UserCommand);
        }

        /// <summary>Breaks the CommandText into the 3 segments (Action, Target, Params) known by the
        /// Command class. Segments are saved for reference. CommandType is also set and saved. </summary>
        /// <param name="CommandText">Command text to be parsed.</param>
        private void ParseCommand(string CommandText)
        {
            const char SPACE = (char)32;
            char[] Delimiter_Space = new char[] { SPACE };
            string[] CommandSegments = CommandText.Split(Delimiter_Space, StringSplitOptions.RemoveEmptyEntries);

            const int MINIMUM_SEGMENTS = 2;

            if (CommandSegments.Length < MINIMUM_SEGMENTS)
            {
                _CommandType = CommandTypes.Invalid;
            }
            else
            {
                _Action = CommandSegments[0];
                _Target = CommandSegments[1];
                _Params = CommandText.Remove(0, (_Action + SPACE + _Target).Length).Trim();

                DetermineCommandType();
            }              
        }

        /// <summary>Sets the CommandType based on the object's current Action value.</summary>
        private void DetermineCommandType() 
        {
            switch (_Action.ToLower())
            {
                case "help":
                    _CommandType = CommandTypes.Help;
                    break;

                case "info":
                    _CommandType = CommandTypes.Info;
                    break;

                case "list":
                    _CommandType = CommandTypes.List;
                    break;

                case "query":
                    _CommandType = CommandTypes.Query;
                    break;

                case "queue":
                    _CommandType = CommandTypes.Queue;
                    break;

                case "roll":
                    _CommandType = CommandTypes.Roll;
                    break;

                case "run":
                    _CommandType = CommandTypes.Run;
                    break;

                case "search":
                    _CommandType = CommandTypes.Search;
                    break;

                default:
                    _CommandType = CommandTypes.Invalid;
                    break;
            }
        }

        #region Properties

        // Type of Command to be performed.
        private CommandTypes _CommandType;
        public CommandTypes CommandType
        { get { return _CommandType; } }

        // Intended Command Action. This should be a string representation of the _CommandType.
        private string _Action;
        public string Action
        { get { return _Action; } }

        // Target that the Command is intended to be performed against.
        private string _Target;
        public string Target
        { get { return _Target; } }

        // Additional parameters for executing the command.
        private string _Params;
        public string Params
        { get { return _Params; } }

        #endregion
    }
}
